package org.domain.sigepac.Entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

@Entity
@Table(name = "tb_frequencia")
public class Frequencia extends EntidadeAbstrata implements Comparable<Frequencia> {

	private static final long serialVersionUID = 2624449853716240343L;


	public Frequencia() {
		this.id.setData(new Date());
	}

	@EmbeddedId
	private FrequenciaId id=new FrequenciaId();

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_aluno", insertable = false, updatable = false)
	private Aluno aluno;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_turma", insertable = false, updatable = false)
	private Turma turma;


	@Column(name="presente",nullable=false)
	@NotNull
	private boolean presente;

	public void setData(Date data){
		this.id.setData(data);
	}

	public Date getData(){
		return this.id.getData();
	}

	public boolean isPresente() {
		return presente;
	}

	public void setPresente(boolean presente) {
		this.presente = presente;
	}

	public Serializable getId() {
		// TODO Auto-generated method stub
		return id;
	}

	public Aluno getAluno() {
		return aluno;
	}

	public void setAluno(Aluno aluno) {
		this.aluno = aluno;
		((FrequenciaId) getId()).setIdAluno((Integer)aluno.getId());
	}

	public Turma getTurma() {
		return turma;
	}

	public void setTurma(Turma turma) {
		this.turma = turma;
		((FrequenciaId) getId()).setIdTurma((Integer)turma.getId());

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((aluno == null) ? 0 : aluno.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + (presente ? 1231 : 1237);
		result = prime * result + ((turma == null) ? 0 : turma.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Frequencia other = (Frequencia) obj;
		if (aluno == null) {
			if (other.aluno != null)
				return false;
		} else if (!aluno.equals(other.aluno))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (presente != other.presente)
			return false;
		if (turma == null) {
			if (other.turma != null)
				return false;
		} else if (!turma.equals(other.turma))
			return false;
		return true;
	}

	public int compareTo(Frequencia o) {
		// TODO Auto-generated method stub
		return this.getAluno().getNome().compareTo(o.getAluno().getNome());
	}


}
